package com.cs.bd.ad;

import android.content.Context;

import com.cs.bd.ad.self.timer.NtpTrustedTime;
import com.cs.bd.utils.LogUtils;

public class TimerUtils {

    private static boolean mIsRefreshing, mIsRefreshSuccess;

    public static boolean forceRefresh(Context context) {
        return NtpTrustedTime.getInstance(context).forceRefresh();
    }

    public static long currentTimeMillis(final Context context) {
        if (!mIsRefreshSuccess) {
            if (!mIsRefreshing) {
                mIsRefreshing = true;
                new Thread() {

                    @Override
                    public void run() {
                        int count = 0;
                        while (count < 3) {
                            count++;
                            if (forceRefresh(context)) {
                                mIsRefreshSuccess = true;
                                break;
                            }
                        }
                        mIsRefreshing = false;
                        LogUtils.d("mIsRefreshSuccess : " + mIsRefreshSuccess);
                    }
                }.start();
            }
        }
        try {
            return NtpTrustedTime.getInstance(context).currentTimeMillis();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return System.currentTimeMillis();
    }

}
